package utilities.print.menu.execute.userPortal;

import employee.User;
import utilities.JDBC.Database;
import utilities.JDBC.sql.DeleteUser;
import utilities.JDBC.sql.SelectUserName;
import utilities.print.log.LogMessage;
import utilities.print.menu.MenuExecute;
import utilities.print.menu.execute.Verify;

import java.sql.ResultSet;
import java.util.Scanner;

import static utilities.print.menu.Menu.line;

public class Delete implements MenuExecute {
    private final Database<User> database = new Database<>();
    private final LogMessage Log = new LogMessage("userPortal.delete");
    /**
     * 启动菜单功能类方法
     *
     * @throws Exception 可能产生的异常
     */
    @Override
    public void execute() throws Exception {
        Scanner in = new Scanner(System.in);

        System.out.println("删除账号");
        line();
        PrintList.printDatabaseUserInfo();
        System.out.print("输入要更改的账号名字: ");
        String name = in.next();
        line();

        ResultSet set = database.executeSelect(new SelectUserName(name));
        if (set.next()){
            //删除后添加修改后的信息
            database.executeUpdate(new DeleteUser(name));

            System.out.println("删除账号成功!");
            System.out.printf("删除的账号名 名字:%s\n", name);
            database.close();
            Log.print("在数据库删除账号: " + name);
            Log.closeOutputFile();
            PrintList.printDatabaseUserInfo();
        }else {
            System.out.printf("找不到名字是 %s 的账号!\n",name);
            line();
        }
        Verify.pressKey("输入任意内容退出");
    }
}
